package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sxt.domain.SxtJueseCaidan;
import com.ruoyi.sxt.domain.SxtYonghuJuese;
import com.ruoyi.sxt.mapper.SxtJueseCaidanMapper;
import com.ruoyi.sxt.mapper.SxtJueseMapper;
import com.ruoyi.sxt.mapper.SxtYonghuJueseMapper;
import com.ruoyi.sxt.service.ISxtJueseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-26
 */
@Service
public class SxtJueseServiceImpl implements ISxtJueseService
{
    @Autowired
    private SxtJueseMapper sxtJueseMapper;

    @Autowired
    private SxtYonghuJueseMapper sxtYonghuJueseMapper;

    @Autowired
    private SxtJueseCaidanMapper sxtJueseCaidanMapper;

    /**
     * 查询角色信息
     * 
     * @param roleId 角色信息主键
     * @return 角色信息
     */
    @Override
    public SxtJuese selectSxtJueseByRoleId(Long roleId)
    {
        return sxtJueseMapper.selectSxtJueseByRoleId(roleId);
    }

    /**
     * 查询角色信息列表
     * 
     * @param sxtJuese 角色信息
     * @return 角色信息
     */
    @Override
    public List<SxtJuese> selectSxtJueseList(SxtJuese sxtJuese)
    {
        return sxtJueseMapper.selectSxtJueseList(sxtJuese);
    }

    /**
     * 新增角色信息
     * 
     * @param sxtJuese 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSxtJuese(SxtJuese sxtJuese)
    {
        sxtJuese.setCreateTime(DateUtils.getNowDate());
        int rows = sxtJueseMapper.insertSxtJuese(sxtJuese);
        insertSxtJueseCaidan(sxtJuese);
        return rows;
    }

    public void insertSxtJueseCaidan(SxtJuese sxtJuese){
        Long[] menuIds = Convert.toLongArray(sxtJuese.getMenuIds());
        if(menuIds != null && menuIds.length > 0){
            List<SxtJueseCaidan> jueseCaidanList = new ArrayList<>();
            for(Long menuId : menuIds){
                SxtJueseCaidan sxtJueseCaidan = new SxtJueseCaidan();
                sxtJueseCaidan.setRoleId(sxtJuese.getRoleId());
                sxtJueseCaidan.setMenuId(menuId);
                jueseCaidanList.add(sxtJueseCaidan);
            }
            sxtJueseCaidanMapper.batchJueseCaidan(jueseCaidanList);
        }
    }

    @Override
    public int insertAuthUsers(Long roleId, String userIds) {
        Long [] users = Convert.toLongArray(userIds);
        List<SxtYonghuJuese> sxtYonghuJueseList = new ArrayList<>();
        for(Long userId : users){
            SxtYonghuJuese sxtYonghuJuese = new SxtYonghuJuese();
            sxtYonghuJuese.setUserId(userId);
            sxtYonghuJuese.setRoleId(roleId);
            sxtYonghuJueseList.add(sxtYonghuJuese);
        }
        return sxtYonghuJueseMapper.batchUserRole(sxtYonghuJueseList);
    }

    @Override
    public int deleteSxtYonghuJueseInfo(SxtYonghuJuese sxtYonghuJuese) {
        return sxtYonghuJueseMapper.deleteSxtYonghuJueseInfo(sxtYonghuJuese);
    }

    /**
     * 修改角色信息
     * 
     * @param sxtJuese 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSxtJuese(SxtJuese sxtJuese)
    {
        sxtJueseCaidanMapper.deleteSxtJueseCaidanByRoleId(sxtJuese.getRoleId());
        insertSxtJueseCaidan(sxtJuese);
        sxtJuese.setUpdateTime(DateUtils.getNowDate());
        return sxtJueseMapper.updateSxtJuese(sxtJuese);
    }

    /**
     * 批量删除角色信息
     * 
     * @param roleIds 需要删除的角色信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtJueseByRoleIds(String roleIds)
    {
        return sxtJueseMapper.deleteSxtJueseByRoleIds(Convert.toStrArray(roleIds));
    }

    /**
     * 删除角色信息信息
     * 
     * @param roleId 角色信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtJueseByRoleId(Long roleId)
    {
        return sxtJueseMapper.deleteSxtJueseByRoleId(roleId);
    }
}
